package com.ic.es.service.impl;


import com.ic.domain.EsPassingVehicleRecord;
import com.ic.es.service.EsPassingVehicleService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;

@Service
public class EsPassingVehicleServiceImpl implements EsPassingVehicleService {


    private final RestHighLevelClient client;

    @Autowired
    public EsPassingVehicleServiceImpl(RestHighLevelClient client) {
        this.client = client;
    }


    @Override
    public void indexPassingVehicleRecord(EsPassingVehicleRecord record) {
        try {
        String indexName = "passingvehicle-" + LocalDate.now().toString().replace("-", "");
        IndexRequest request = new IndexRequest(indexName)
                .source(record, XContentType.JSON);
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
